﻿layui.use(['table', 'dropdown', 'layer', 'form'], function () {
    var table = layui.table;
    var dropdown = layui.dropdown;
    var layer = layui.layer;
    var form = layui.form;
    var laypage = layui.laypage;

    window.VendorTan = function VendorTan() {
        layer.open({
            type: 1, // page 层类型
            area: ['800px', '500px'],
            title: '新增供应商',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            maxmin: true, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: `
                                 <div style="padding: 32px;">
                                          <form class="layui-form layui-form-pane" action="">
                                           <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">供应商编号:</label>
                                              <div class="layui-input-block">
                                                        <input type="text" id="vendorNo1" lay-verify="required" placeholder="请输入"      autocomplete="off"  class="layui-input">

                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">供应商名称:</label>
                                              <div class="layui-input-inline">
                                                       <input type="text" id="vendorName1" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">状态:</label>
                                              <div class="layui-input-block">
                                                          <select id="vendorState1" lay-filter="aihao">
                    <option value="0">全部</option>
                    <option value="1">等待审核</option>
                    <option value="2">审核成功</option>
                     <option value="3">审核失败</option>
                  </select>

                                              </div>
                                             </div>
                                            <div class="layui-inline">
                                                              <label class="layui-form-label">电话:</label>
                                              <div class="layui-input-inline">
                                                          <input type="text" id="vendorPhone1" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">

                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">传真：</label>
                                              <div class="layui-input-block">
                                                      <input type="text" id="vendorFax" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">邮箱:</label>
                                              <div class="layui-input-inline">
                                                         <input type="text" id="vendorEmail" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input" />

                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">联系人：</label>
                                              <div class="layui-input-block">
                                                      <input type="text" id="vendorContact" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">地址：</label>
                                              <div class="layui-input-inline">
                                                         <input type="text" id="vendorAddress" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input" />

                                              </div>
                                            </div>
                                          </div>
                                          <div class="layui-form-item layui-form-text">
                                              <label class="layui-form-label">描述：</label>
                                              <div class="layui-input-block">
                                               <textarea id="vendorDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
                                           </div>
                                           </div>
                                  </div>
                                  <div class="layui-form-item">
                                            <button class="layui-btn" lay-submit lay-filter="demo2" onclick="VendorAdd()">确认</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                  </div>
                                </form>
                                  </div>`

        });

        form.render()
    }

    window.FanUpdate = function FanUpdate(vendorId) {
        var token = localStorage.getItem("token")
        var model = JSON.stringify({
            vendorId: vendorId
        })
        $.ajax({
            url: '../Vonder/VenderFanDo',
            data: { model: model, token: token },
            type: 'post',
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res)
                $.each(res, function () {
                    $("#vendorId").val(this.vendorId)
                    $("#vendorNo1").val(this.vendorNo)
                    $("#vendorName1").val(this.vendorName)
                    $("#vendorState1").val(this.vendorState)
                    $("#vendorPhone1").val(this.vendorPhone)
                    $("#vendorFax").val(this.vendorFax)
                    $("#vendorEmail").val(this.vendorEmail)
                    $("#vendorContact").val(this.vendorContact)
                    $("#vendorAddress").val(this.vendorAddress)
                    $("#vendorDesc").val(this.vendorDesc)
                    // 设置下拉框默认选择
                    var vendorState = this.vendorState;
                    $("#vendorState1").val(vendorState);
                    layui.form.render('select'); // 重新渲染select表单元素
                })
            }
        })
        layer.open({
            type: 1, // page 层类型
            area: ['800px', '500px'],
            title: '修改设备',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            maxmin: true, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: `
                                        <div style="padding: 32px;">
                                          <form class="layui-form layui-form-pane" action="">
                                           <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">供应商编号:</label>
                                              <div class="layui-input-block">
                                                        <input type="text" id="vendorNo1" lay-verify="required" placeholder="请输入"      autocomplete="off"  class="layui-input">

                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">供应商名称:</label>
                                              <div class="layui-input-inline">
                                                       <input type="text" id="vendorName1" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">状态:</label>
                                              <div class="layui-input-block">
                                                          <select id="vendorState1" lay-filter="aihao">
                    <option value="0">全部</option>
                    <option value="1">等待审核</option>
                    <option value="2">审核成功</option>
                     <option value="3">审核失败</option>
                  </select>

                                              </div>
                                             </div>
                                            <div class="layui-inline">
                                                              <label class="layui-form-label">电话:</label>
                                              <div class="layui-input-inline">
                                                          <input type="text" id="vendorPhone1" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">

                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">传真：</label>
                                              <div class="layui-input-block">
                                                      <input type="text" id="vendorFax" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">邮箱:</label>
                                              <div class="layui-input-inline">
                                                         <input type="text" id="vendorEmail" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input" />

                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">联系人：</label>
                                              <div class="layui-input-block">
                                                      <input type="text" id="vendorContact" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">地址：</label>
                                              <div class="layui-input-inline">
                                                         <input type="text" id="vendorAddress" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input" />

                                              </div>
                                            </div>
                                          </div>
                                          <div class="layui-form-item layui-form-text">
                                              <label class="layui-form-label">描述：</label>
                                              <div class="layui-input-block">
                                               <textarea id="vendorDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
                                           </div>
                                           </div>
                                  </div>
                                  <div class="layui-form-item">
                                            <button class="layui-btn" lay-submit lay-filter="demo2" onclick="Update()">确认</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                  </div>
                                </form>
                                  </div>`

        });
        form.render()
        window.Update = function Update() {
            vendorNo1 = $("#vendorNo1").val()
            vendorName1 = $("#vendorName1").val()
            vendorState = $("#vendorState1").val()
            vendorPhone = $("#vendorPhone1").val()
            vendorFax = $("#vendorFax").val()
            vendorEmail = $("#vendorEmail").val()
            vendorContact = $("#vendorContact").val()
            vendorAddress = $("#vendorAddress").val()
            vendorDesc = $("#vendorDesc").val
            var models = JSON.stringify({
                "vendorId": vendorId,
                "vendorNo": vendorNo1,
                "vendorName": vendorName1,
                "vendorState": parseInt(vendorState),
                "vendorPhone": vendorPhone,
                "vendorFax": vendorFax,
                "vendorEmail": vendorEmail,
                "vendorContact": vendorContact,
                "vendorAddress": vendorAddress,
                "vendorDesc": vendorDesc
            })
            console.log(models)
            var token = localStorage.getItem("token")
            $.ajax({
                url: '../Vonder/VenderupdateDo',
                type: 'post',
                data: { models: models, token: token },
                //contentType: 'application/json',
                success: function (res) {
                    console.log(res)
                    if (res != null) {
                        layer.msg("修改成功", {icon:1})
                        window.location.href = '/Vonder/VenderShow'
                    }
                    else {
                        layer.msg("修改失败", {icon:2})
                    }

                }
            })
        }
    }

    window.VendorAdd = function VendorAdd() {
        vendorNo1 = $("#vendorNo1").val()
        vendorName1 = $("#vendorName1").val()
        vendorState = $("#vendorState1").val()
        vendorPhone = $("#vendorPhone1").val()
        vendorFax = $("#vendorFax").val()
        vendorEmail = $("#vendorEmail").val()
        vendorContact = $("#vendorContact").val()
        vendorAddress = $("#vendorAddress").val()
        vendorDesc = $("#vendorDesc").val
        var model = JSON.stringify({
            "vendorNo": vendorNo1,
            "vendorName": vendorName1,
            "vendorState": parseInt(vendorState),
            "vendorPhone": vendorPhone,
            "vendorFax": vendorFax,
            "vendorEmail": vendorEmail,
            "vendorContact": vendorContact,
            "vendorAddress": vendorAddress,
            "vendorDesc": vendorDesc
        })
        console.log(model)
        var token = localStorage.getItem("token")
        $.ajax({
            url: '../Vonder/VenderAddDo',
            type: 'post',
            data: { model: model, token: token },
            //contentType: 'application/json',
            success: function (res) {
                console.log(res)
                var res = JSON.parse(res)
                if (res > 0) {
                    layer.msg("添加成功", {icon:1})
                    window.location.href = '/Vonder/VenderShow'
                }
                else {
                    layer.msg("添加失败", {icon:2})
                }

            }
        })
    }


    window.VendorBang = function VendorBang() {
        var model = JSON.stringify({

        })
        var token = localStorage.getItem("token")
        $.ajax({
            url: '../Vonder/VenderBangDo',
            type: 'post',
            data: { model: model, token: token },
            //contentType: 'application/json',
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res)
                var counts = `<option value="0" >全部</option>`
                $.each(res, function () {
                    counts += `<option value="${this.vendorTypeId}">${this.vendorTypeName}</option>`
                })
                $("#vendorTypeId").html(counts)
                form.render();
                console.log($("#vendorTypeId"))
            }
        })
    }

    window.Show = function Show() {
        var token = localStorage.getItem("token")
        var model = JSON.stringify({
            vendorNo: $("#vendorNo").val(),
            vendorName: $("#vendorName").val(),
            vendorPhone: $("#vendorPhone").val(),
            vendorTypeId: $("#vendorTypeId").val() || 0
        })
        $.ajax({
            url: '../Vonder/VenderShowDo',
            type: 'post',
            //contentType: 'application/json',
            data: { model: model, token: token },
            success: function (res) {
                res = JSON.parse(res);
                console.log(res)
                // 处理数据并渲染表格
                var cols = [
                    [
                        { field: 'vendorId', type: 'checkbox', fixed: 'left' },
                        { field: 'vendorNo', title: '供应商编号', width: 80 },
                        { field: 'vendorName', title: '供应商名称', width: 120 },
                        { field: 'vendorTypeName', title: '类型', width: 120 },
                        { field: 'vendorPhone', title: '电话', width: 120 },
                        { field: 'vendorFax', title: '传真', width: 120 },
                        { field: 'vendorEmail', title: 'Email', width: 120 },
                        { field: 'vendorContact', title: '联系人', width: 120 },
                        { field: 'vendorAddress', title: '地址', width: 120 },
                        { field: 'createTime', title: '创建时间', width: 120 },
                        {
                            fixed: 'right', title: '操作', width: 150, templet: function (d) {
                                return '<a href="#" class="layui-btn layui-btn-xs layui-btn-primary" onclick="FanUpdate(' + d.vendorId + ')">编辑</a>' +
                                    '<a href="#" class="layui-btn layui-btn-xs layui-btn-danger" onclick="IsDel(' + d.vendorId + ')">删除</a>';
                            }
                        }
                    ]
                ];
                table.render({
                    elem: '#test',
                    data: res,
                    cols: cols,
                    page: true,
                    limits: [5, 10, 15, 20],
                    limit: 5,//设置每页显示的条数。
                });
            },
        });
    }
    $(function () {
        Show()
        VendorBang()
    })

    window.IsDel = function IsDel(vendorId) {
        var token = localStorage.getItem("token")
        var model = JSON.stringify({
            vendorId: vendorId
        })
        layer.confirm("确认删除吗", function () {
            $.ajax({
                url: '../Vonder/VenderIsDelDo',
                data: { model: model, token: token },
                type: 'post',
                success: function (res) {
                    console.log(res);
                    var res = JSON.parse(res)
                    if (res > 0) {
                        layer.msg("删除成功")
                        Show();
                    }
                    else {
                        layer.msg("删除失败")
                    }
                }
            })
        })
    }


    window.Output = function Output() {
        var data = [];

        //直接获取表格正文数据并添加到data数组，跳过表头（thead）获取
        $('table.layui-table tbody tr').each(function () {
            var rowData = [];
            $(this).find('td').each(function () {
                rowData.push($(this).text());
            });
            data.push(rowData);
        });

        //将数据转换为Worksheet对象
        var worksheet = XLSX.utils.aoa_to_sheet(data);

        //创建一个新的Workbook对象，并将Worksheet添加进去
        var workbook = XLSX.utils.book_new();
        XLSX.utils.book_append_sheet(workbook, worksheet, "Sheet1");

        //将Workbook对象转换为二进制数据
        var wbout = XLSX.write(workbook, { bookType: 'xlsx', type: 'binary' });

        //将二进制数据转换为Blob对象
        function s2ab(s) {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for (var i = 0; i < s.length; i++) {
                view[i] = s.charCodeAt(i) & 0xff;
            }
            return new Blob([buf], { type: 'application/octet-stream' });
        }
        var blob = s2ab(wbout);

        //创建一个下载链接并触发点击事件来下载文件
        var url = URL.createObjectURL(blob);
        var link = document.createElement('a');
        link.href = url;
        link.download = '设备Device.xlsx';
        link.click();

        //释放URL对象
        URL.revokeObjectURL(url);
    }

    window.IsdelAll = function IsdelAll() {
        var checkStatus = table.checkStatus('test');
        var checkedData = checkStatus.data;
        var vendorId = [];
        for (var i = 0; i < checkedData.length; i++) {
            vendorId.push(checkedData[i].vendorId);
        }
        if (vendorId == 0) {
            layer.msg("至少选择一条数据", { icon: 2 })
            return;
        }
        var model = JSON.stringify({
            ins: vendorId
        });
        console.log(model);
        var token = localStorage.getItem("token")
        layer.confirm("确认删除吗", function () {
            $.ajax({
                url: '../Vonder/VenderIsdelAllDo',
                data: { model: model, token: token },
                type: 'post',
                success: function (res) {
                    console.log(res);
                    var res = JSON.parse(res)
                    if (res > 0) {
                        layer.msg("删除成功")
                        Show();
                    }
                    else {
                        layer.msg("删除失败")
                    }
                }
            })
        })
    }

    window.FanUpdateAll = function FanUpdateAll() {
        var token = localStorage.getItem("token")
     
        var checkStatus = table.checkStatus('test');
        var checkedData = checkStatus.data;
        var vendorId = [];
        for (var i = 0; i < checkedData.length; i++) {
            vendorId.push(checkedData[i].vendorId);
        }
        var model = JSON.stringify({
            vendorId: vendorId[0]
        })
        $.ajax({
            url: '../Vonder/VenderFanDo',
            data: { model: model, token: token },
            type: 'post',
            success: function (res) {
                var res = JSON.parse(res)
                console.log(res)
                $.each(res, function () {
                    $("#vendorId").val(this.vendorId)
                    $("#vendorNo1").val(this.vendorNo)
                    $("#vendorName1").val(this.vendorName)
                    $("#vendorState1").val(this.vendorState)
                    $("#vendorPhone1").val(this.vendorPhone)
                    $("#vendorFax").val(this.vendorFax)
                    $("#vendorEmail").val(this.vendorEmail)
                    $("#vendorContact").val(this.vendorContact)
                    $("#vendorAddress").val(this.vendorAddress)
                    $("#vendorDesc").val(this.vendorDesc)
                    // 设置下拉框默认选择
                    var vendorState = this.vendorState;
                    $("#vendorState1").val(vendorState);
                    layui.form.render('select'); // 重新渲染select表单元素
                })
            }
        })
        layer.open({
            type: 1, // page 层类型
            area: ['800px', '300px'],
            title: '修改供应商',
            shade: 0.6, // 遮罩透明度
            shadeClose: true, // 点击遮罩区域，关闭弹层
            maxmin: true, // 允许全屏最小化
            anim: 0, // 0-6 的动画形式，-1 不开启
            content: `
                                        <div style="padding: 32px;">
                                          <form class="layui-form layui-form-pane" action="">
                                           <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">供应商编号:</label>
                                              <div class="layui-input-block">
                                                        <input type="text" id="vendorNo1" lay-verify="required" placeholder="请输入"      autocomplete="off"  class="layui-input">

                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">供应商名称:</label>
                                              <div class="layui-input-inline">
                                                       <input type="text" id="vendorName1" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">状态:</label>
                                              <div class="layui-input-block">
                                                          <select id="vendorState1" lay-filter="aihao">
                    <option value="0">全部</option>
                    <option value="1">等待审核</option>
                    <option value="2">审核成功</option>
                     <option value="3">审核失败</option>
                  </select>

                                              </div>
                                             </div>
                                            <div class="layui-inline">
                                                              <label class="layui-form-label">电话:</label>
                                              <div class="layui-input-inline">
                                                          <input type="text" id="vendorPhone1" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">

                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">传真：</label>
                                              <div class="layui-input-block">
                                                      <input type="text" id="vendorFax" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">邮箱:</label>
                                              <div class="layui-input-inline">
                                                         <input type="text" id="vendorEmail" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input" />

                                              </div>
                                            </div>
                                          </div>
                                            <div class="layui-form-item">
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">联系人：</label>
                                              <div class="layui-input-block">
                                                      <input type="text" id="vendorContact" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input">
                                              </div>
                                            </div>
                                            <div class="layui-inline">
                                                      <label class="layui-form-label">地址：</label>
                                              <div class="layui-input-inline">
                                                         <input type="text" id="vendorAddress" lay-verify="required" placeholder="请输入"   autocomplete="off"  class="layui-input" />

                                              </div>
                                            </div>
                                          </div>
                                          <div class="layui-form-item layui-form-text">
                                              <label class="layui-form-label">描述：</label>
                                              <div class="layui-input-block">
                                               <textarea id="vendorDesc" placeholder="请输入内容" class="layui-textarea"></textarea>
                                           </div>
                                           </div>
                                  </div>
                                  <div class="layui-form-item">
                                            <button class="layui-btn" lay-submit lay-filter="demo2" onclick="UpdateAll()">确认</button>
                                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                                  </div>
                                </form>
                                  </div>`

        });
        form.render()
        window.UpdateAll = function UpdateAll() {
            vendorNo1 = $("#vendorNo1").val()
            vendorName1 = $("#vendorName1").val()
            vendorState = $("#vendorState1").val()
            vendorPhone = $("#vendorPhone1").val()
            vendorFax = $("#vendorFax").val()
            vendorEmail = $("#vendorEmail").val()
            vendorContact = $("#vendorContact").val()
            vendorAddress = $("#vendorAddress").val()
            vendorDesc = $("#vendorDesc").val
            var models = JSON.stringify({
                "vendorId": vendorId[0],
                "vendorNo": vendorNo1,
                "vendorName": vendorName1,
                "vendorState": parseInt(vendorState),
                "vendorPhone": vendorPhone,
                "vendorFax": vendorFax,
                "vendorEmail": vendorEmail,
                "vendorContact": vendorContact,
                "vendorAddress": vendorAddress,
                "vendorDesc": vendorDesc,
                "isDelete":false
            })
            console.log(models)
            var token = localStorage.getItem("token")
            $.ajax({
                url: '../Vonder/VenderupdateDo',
                type: 'post',
                data: { models: models, token: token },
                //contentType: 'application/json',
                success: function (res) {
                    console.log(res)
                    if (res != null) {
                        layer.msg("修改成功", {icon:1})
                        window.location.href = '/Vonder/VenderShow'
                    }
                    else {
                        layer.msg("修改失败", {icon:2})
                    }

                }
            })
        }
    }
});